package com.baijiayun.liveuibase.widgets.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import android.util.AttributeSet;

import com.baijiayun.liveuibase.base.BaseUIConstant.ResStateType;
import com.baijiayun.liveuibase.R;
import com.baijiayun.liveuibase.utils.DisplayUtils;
import com.baijiayun.liveuibase.utils.ThemeDataUtil;
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder;
import com.baijiayun.liveuibase.utils.drawable.StateListDrawableBuilder;

/**
 * Created by pengwenjian on 2020/6/30
 * Describe:
 */
public class StateTextView extends AppCompatTextView {
    private int stateTextColor,unStateTextColor;
    private ResStateType stateType;
    private int bgType;
    private Context context;

    public StateTextView(Context context) {
        this(context,null);
    }

    public StateTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public StateTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attrs) {
        this.context = context;
        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BJYStateTextView);
        stateTextColor = typedArray.getResourceId(R.styleable.BJYStateTextView_bjlive_stateTextColor,0);
        unStateTextColor = typedArray.getResourceId(R.styleable.BJYStateTextView_bjlive_unStateTextColor, 0);
        stateType = ResStateType.valueOf(typedArray.getInt(R.styleable.BJYStateTextView_bjlive_stateType, 0));
        bgType = typedArray.getInt(R.styleable.BJYStateTextView_bjlive_bgType, 0);
        typedArray.recycle();

        notifyView();
    }

    public void notifyView() {
        if (stateType == null) {
            if (stateTextColor != 0) {
                setTextColor(ThemeDataUtil.getColorFromThemConfigByColorId(getContext(), stateTextColor));
            }
            if (unStateTextColor != 0) {
                setTextColor(ThemeDataUtil.getColorFromThemConfigByColorId(getContext(), unStateTextColor));
            }
        } else {
            setTextColor(DisplayUtils.getColorStateList(
                    ThemeDataUtil.getColorFromThemConfigByColorId(getContext(), stateTextColor),
                    ThemeDataUtil.getColorFromThemConfigByColorId(getContext(), unStateTextColor),
                    stateType));
        }
        //直接传drawable不实用
        if (bgType == 1) {
            //positive
            setBackground(new DrawableBuilder()
                    .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color))
                    .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                    .build());
            setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context,R.attr.base_theme_dialog_positive_text_color));
        } else if (bgType == 2) {
            //negative
            setBackground(new DrawableBuilder()
                    .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_dialog_negative_bg_color))
                    .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                    .build());
            setTextColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context,R.attr.base_theme_dialog_negative_text_color));
        } else if (bgType == 3) {
            //select
            final Drawable selecteDrawable = new DrawableBuilder()
                    .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color))
                    .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                    .build();
            final Drawable drawable = new DrawableBuilder()
                    .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_dialog_negative_bg_color))
                    .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                    .build();
            setBackground( new StateListDrawableBuilder()
                    .normal(drawable)
                    .selected(selecteDrawable)
                    .build());
        } else if (bgType == 4) {
            //enable
            final Drawable drawable = new DrawableBuilder()
                    .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color))
                    .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                    .build();
            final Drawable disableDrawable = new DrawableBuilder()
                    .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_dialog_negative_bg_color))
                    .cornerRadius(context.getResources().getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
                    .build();
            setBackground( new StateListDrawableBuilder()
                    .normal(drawable)
                    .disabled(disableDrawable)
                    .build());
        }
    }

    public int getStateTextColor() {
        return stateTextColor;
    }

    /**
     * 更改 状态 时 文字颜色，更改完成后请调用 {@link #notifyView()} 更新视图
     */
    public StateTextView setStateTextColor(int stateTextColor) {
        this.stateTextColor = stateTextColor;
        return this;
    }

    public int getUnStateTextColor() {
        return unStateTextColor;
    }

    /**
     * 更改 非状态 时 文字颜色，更改完成后请调用 {@link #notifyView()} 更新视图
     */
    public StateTextView setUnStateTextColor(int unStateTextColor) {
        this.unStateTextColor = unStateTextColor;
        return this;
    }

    public ResStateType getStateType() {
        return stateType;
    }

    /**
     * 更改 状态 类型，更改完成后请调用 {@link #notifyView()} 更新视图
     */
    public StateTextView setStateType(ResStateType stateType) {
        this.stateType = stateType;
        return this;
    }
}
